package com.qingcheng.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.system.Admin;
import com.qingcheng.service.system.AdminService;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Reference
    private AdminService adminService;

    @GetMapping("/findAll")
    public List<Admin> findAll(){
        return adminService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Admin> findPage(int page, int size){
        return adminService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Admin> findList(@RequestBody Map<String,Object> searchMap){
        return adminService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Admin> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  adminService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public Map<String, Object> findById(Integer id){
        return adminService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Map<String,Object> ruleForm){

        Admin admin = new Admin();
        admin.setLoginName((String) ruleForm.get("username"));
        admin.setPassword((String) ruleForm.get("pass"));
        admin.setStatus("1");
        List<Integer> roleIds = (List<Integer>) ruleForm.get("roleIds");

        adminService.add(admin,roleIds);
        return new Result();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Map<String,Object> ruleForm){

        String loginName = SecurityContextHolder.getContext().getAuthentication().getName();//登录名
        String oldPassword = (String) ruleForm.get("oldPass");//旧密码
//        System.out.println(ruleForm.get("oldPass"));
//
//        System.out.println(oldPassword);

        Map map = new HashMap();
        map.put("loginName",loginName);
        Admin admin = (Admin) adminService.findList(map).get(0);
        Boolean checkpw = BCrypt.checkpw(oldPassword,admin.getPassword());//判断旧密码是否正确
        if(!checkpw){
            throw new RuntimeException("旧密码输入错误，请重新输入");
        }

        adminService.update2(ruleForm);

        String newLoginName = (String) ruleForm.get("username");
        System.out.println("newLoginName:"+newLoginName);
        System.out.println("loginName:"+loginName);
        if(!newLoginName.equals(loginName)){
            return new Result(2,"修改目前登录者的登录账号,需要重新登录");
        }
        return new Result();
    }

    @GetMapping("/delete")
    public Result delete(Integer id){
        //获取登录名
        String loginName = SecurityContextHolder.getContext().getAuthentication().getName();
        Admin admin = adminService.findAdminById(id);
        System.out.println("loginName:"+loginName);
        System.out.println("admin_username:"+admin.getLoginName());
        if(admin.getLoginName().equals(loginName)){
            return new Result(1,"该账号正在登录,不可以删除");
        }
        adminService.delete(id);
        return new Result();
    }

    /////////////////////////////////////////////////////////////////

    /**
     * 修改管理员密码
     * @param ruleForm
     * @return
     */
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Map<String,String> ruleForm){

        String loginName = SecurityContextHolder.getContext().getAuthentication().getName();//登录名
        String oldPassword = ruleForm.get("oldPass");//旧密码

        Map map = new HashMap();
        map.put("loginName",loginName);
        Admin admin = (Admin) adminService.findList(map).get(0);
        Boolean checkpw = BCrypt.checkpw(oldPassword,admin.getPassword());//判断旧密码是否正确
        if(!checkpw){
            throw new RuntimeException("旧密码输入错误，请重新输入");
        }

        String password=ruleForm.get("pass");//获取新密码

        String gensalt = BCrypt.gensalt();//创建随机盐
        String newPassword = BCrypt.hashpw(password,gensalt);//生产密码
        admin.setPassword(newPassword);//设置新密码

        adminService.update(admin);
        return new Result();
    }

    /**
     * 检查是否有重复的用户名
     * @param loginName
     * @return
     */
    @GetMapping("/checkRepeatName")
    public Result checkRepeatName(String loginName){
        //根据该用户名查询
        Map map = new HashMap();
        map.put("loginName",loginName);
        List adminList = adminService.findList(map);
        if(adminList.size()==0){
            return new Result();
        }else{
            return new Result(1,"用户名重复");
        }
    }


}
